<?php

namespace App\Http\Controllers;

use App\Entities\OrderCommentEntity;
use App\Entities\OrderCommentStarEntity;
use App\Entities\OrderCommentLabelEntity;
use App\VO\UserOrders\IndexOrderModelVO;
use App\ServicesNew\OrderService;
use Illuminate\Support\Facades\Log;
use App\BO\OrderStatus\OrderStatus;
use App\Entity\OrderEntity;
use App\Exceptions\InvalidRequestException;
use App\Http\Requests\SendReviewRequest;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\VO\UserOrder\UserOrderCommentShowVO;
use App\VO\UserOrder\MetaInfo\UserOrderFactory;
use Illuminate\Http\Request;

class ReviewController extends BaseController
{
    /**
     * 评价页面
     * @param Order $order
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws InvalidRequestException
     */
    public function comment(int $orderId, OrderService $orderService)
    {
        $orderEntity = $orderService->findOrderEntityByIdAndUserId($orderId, $this->getUserId());
        if (is_null($orderEntity)) {
            return $this->showJsonResult(500, '订单不存在');
        }

        $orderStatus = new OrderStatus($orderEntity);
        if (!$orderStatus->isWaitingComment()) {
            return $this->showJsonResult(500, '该订单不是可评价状态，不可评价');
        }

        $userOrderCommentShowVO = new UserOrderCommentShowVO();
        $userOrderCommentShowVO->setComment(UserOrderFactory::getOrderCommentInfo($orderEntity));

        return $this->successJsonResult($userOrderCommentShowVO);

    }

    /**
     * 保存评价
     * @param SendReviewRequest $request
     * @throws \Exception
     */
    public function doComment(Request $request, OrderService $orderService)
    {
        $data = $request->getContent();
        $data = json_decode($data,true);
        $orderId = $data['order_id'];
        $orderEntity = $orderService->findOrderEntityByIdAndUserId($orderId, $this->getUserId());
        if (is_null($orderEntity)) {
            return $this->showJsonResult(500, '订单不存在');
        }

        Log::info('保存订单开始，order_id = ' . $orderId);
        $orderStatus = new OrderStatus($orderEntity);
        if ($orderStatus->reviewed()) {
            return $this->showJsonResult(500, '该订单已评价，不可重复提交');
        }

        DB::beginTransaction();
        $this->setComment($orderEntity, $data['description']); //评价内容
        $this->setStar($orderEntity, $data['stars']); //评价星级
        $this->setLabel($orderEntity, $data['tags']); //评价标签
        $orderStatus->getOrderStateContext()->comment();
        DB::commit();
        Log::info('order_id = ' . $orderId. ' 订单是价状态转为已经评价');

        return $this->successJsonResult();
    }

    /**
     * @param OrderEntity $order
     * @param $description
     */
    private function setComment($order, $description)
    {
        // $data['order_id'] = $order->getId();
        // $data['oa_gid'] = $order->getOaGid();
        // $data['content'] = $description;
        // $data['created_at'] = Carbon::now()->toDateTimeString();
        // DB::table("order_comment")->insert($data);
        DB::beginTransaction();
        $orderCommentEntiy = OrderCommentEntity::getNewInstance();
        $orderCommentEntiy->setOrderId($order->getId());
        $orderCommentEntiy->setOaGid($order->getOaGid());
        $orderCommentEntiy->setContent($description);
        $orderCommentEntiy->setCreatedAt(Carbon::now()->toDateTimeString());
        $orderCommentEntiy->save();
        DB::commit();
    }

    /**
     * @param OrderEntity $order
     * @param $stars
     */
    private function setStar($order, $stars)
    {
        DB::beginTransaction();
        foreach ($stars as $key => $row) {
            $orderCommentStarEntity= OrderCommentStarEntity::getNewInstance();
            $orderCommentStarEntity->setOrderId($order->getId());
            $orderCommentStarEntity->setOaGid($order->getOaGid());
            $orderCommentStarEntity->setServiceTypeId($key);
            $orderCommentStarEntity->setServiceTypeName($row['name']);
            $orderCommentStarEntity->setStarNum($row['star']);
            $orderCommentStarEntity->setCreatedAt(Carbon::now()->toDateTimeString());
            $orderCommentStarEntity->save();
        }
        DB::commit();
    }

    /**
     * @param OrderEntity $order
     * @param $tags
     */
    private function setLabel($order, $tags)
    {
        DB::beginTransaction();
        foreach ($tags as $key => $row) {
            $orderCommentLabelEntity= OrderCommentLabelEntity::getNewInstance();
            $orderCommentLabelEntity->setOrderId($order->getId());
            $orderCommentLabelEntity->setOaGid($order->getOaGid());
            $orderCommentLabelEntity->setLabelId($row['id']);
            $orderCommentLabelEntity->setLabelName($row['name']);
            $orderCommentLabelEntity->setCreatedAt(Carbon::now()->toDateTimeString());
            $orderCommentLabelEntity->save();
        }
        DB::commit();
    }

}
